package breeze.framework.os;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class OS {

	public static String getHostIp(){
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			return addr.getHostAddress().toString();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		//can not get server ip correctly,will use '127.0.0.1' instead .
		return "127.0.0.1";
	}
	
	public static List<String> getLocalIPList() {
	    List<String> ipList = new ArrayList<String>();
	    try {
	        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
	        NetworkInterface networkInterface;
	        Enumeration<InetAddress> inetAddresses;
	        InetAddress inetAddress;
	        String ip;
	        while (networkInterfaces.hasMoreElements()) {
	            networkInterface = networkInterfaces.nextElement();
	            inetAddresses = networkInterface.getInetAddresses();
	            while (inetAddresses.hasMoreElements()) {
	                inetAddress = inetAddresses.nextElement();
	                if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
	                    ip = inetAddress.getHostAddress();
	                    ipList.add(ip);
	                }
	            }
	        }
	    } catch (SocketException e) {
	        e.printStackTrace();
	    }
	    return ipList;
	}
	
	public static boolean isLinux() {
		String osName = System.getProperties().getProperty("os.name");
		if (osName.startsWith("Linux")) {
			return true;
        } else {
            return false;
        }
	}
	
	public static void main(String[] args) {
		System.out.println(getLocalIPList());
	}
}
